<template>
  <div class="change-password-container">
    <el-card class="password-card">
      <template #header>
        <div class="card-header">
          <span>修&nbsp;&nbsp;改&nbsp;&nbsp;密&nbsp;&nbsp;码</span>
        </div>
      </template>

      <el-form
        ref="passwordFormRef"
        :model="passwordForm"
        :rules="passwordRules"
        label-width="100px"
        class="password-form"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input
            v-model="passwordForm.oldPassword"
            type="password"
            placeholder="请输入原密码"
            show-password
          />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="passwordForm.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="passwordForm.confirmPassword"
            type="password"
            placeholder="请确认新密码"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSubmit">提交</el-button>
          <el-button @click="resetForm" class="yellow-button">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import { updatePassword } from "@/request/request";
import { useRouter } from "vue-router";

const router = useRouter();

const passwordFormRef = ref(null);
const passwordForm = reactive({
  oldPassword: "",
  newPassword: "",
  confirmPassword: "",
});

const passwordRules = {
  oldPassword: [
    { required: true, message: "请输入原密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: "请确认新密码", trigger: "blur" },
  ],
};

// 提交表单
const handleSubmit = async () => {
  if (!passwordFormRef.value) return;
  if (passwordForm.newPassword !== passwordForm.confirmPassword) {
    ElMessage.error("两次输入密码不一致");
    return;
  }
  if (passwordFormRef.value.validate()) {
    const res = await updatePassword(passwordForm);
    console.log(res);
    if (res.code == 200) {
      ElMessage.success("密码修改成功");
      resetForm();
      localStorage.removeItem("token");
      router.push("/login");
      ElMessage.success("请重新登录");
    } else {
      ElMessage.error(res.message);
    }
  }
};

// 重置表单
const resetForm = () => {
  if (passwordFormRef.value) {
    passwordFormRef.value.resetFields();
  }
  Object.assign(passwordForm, {
    oldPassword: "",
    newPassword: "",
    confirmPassword: "",
  });
};
</script>

<style scoped>
.change-password-container {
  padding: 20px;
  background-color: #f5f5f5; /* 奶白色背景 */
}

.password-card {
  max-width: 600px;
  margin: 0 auto;
  background-color: #fff; /* 奶白色背景 */
}

.card-header {
  font-size: 16px;
  font-weight: bold;
  text-align: center; /* 居中文本 */
}

.password-form {
  padding: 20px 0;
}

.password-tips {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
  line-height: 1.4;
}

.el-button {
  margin-right: 12px;
}

.yellow-button {
  background-color: #e6a23c; /* 黄色背景 */
  border-color: #e6a23c; /* 黄色边框 */
  color: white; /* 白色文字 */
}

.yellow-button:hover {
  background-color: #d8842d; /* 鼠标悬停时的黄色背景 */
  border-color: #d8842d; /* 鼠标悬停时的黄色边框 */
}

:deep(.el-form-item__content) {
  max-width: 360px;
}
</style>
